package com.ymatou.paygetway.testcase.old;

import java.math.BigDecimal;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.payment.parameter.AcquireRefundBean;
import com.ymatou.paygetway.parameter.ApplePayRefundNotifyBean;
import com.ymatou.paygetway.parameter.BaoFuRefundNotifyBean;
import com.ymatou.paygetway.serivce.NotifyCall;
import com.ymatou.paygetway.serivce.RefundNotifyCall;
import com.ymatou.iapi.payment.parameter.*;
import com.ymatou.iapi.payment.service.*;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RSAUtils;
import com.ymt.util.DataManager;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.data.Sign;
import com.ymttest.business.data.applepaysign.ApplePayConstants;
import com.ymttest.business.data.applepaysign.Hex;
import com.ymttest.business.data.applepaysign.SecurityUtil;
import com.ymttest.business.service.PaymentProxyCallService;
import com.ymttest.database.model.PPBussinessOrder;
import com.ymttest.database.model.PPPayment;
import com.ymttest.database.model.RefundMiscRequestLog;
import com.ymttest.database.model.RefundRequest;
import com.ymttest.database.sqlwapper.PaymentDefaultWapper;
import com.ymttest.utils.BaofooSignature;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 退款回调 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_RefundNotify {
	private static RefundNotifyBean refundnotifyBean;
	private static RefundNotifyCall refundnotifyCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("退款回调");
	}

	@Before
	public void caseUp() {
		refundnotifyBean = new RefundNotifyBean();
		refundnotifyCall = new RefundNotifyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(refundnotifyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	// @Ignore
	public void Tc_RefundNotify_001() {
		Logger.start(true, "10支付宝PC支付同步回调成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			refundnotifyBean = initRefundNotifyBean(aqb);
			String send = aliPayRefundNotifyToSend(refundnotifyBean, aqb);

			refundnotifyCall = new RefundNotifyCall("10");
			refundnotifyCall.setData(send);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("success", refundnotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb, refundnotifyBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_002() {
		Logger.start(true, "13支付宝APP支付同步回调成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("13");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());

			refundnotifyBean = initRefundNotifyBean(aqb);

			// 排序加签名
			String src = FormatUtil.beanToHttpString(refundnotifyBean, false);
			String sign = new Sign().SignSHA1WithRSA(src);
			refundnotifyBean.setSign(sign);
			refundnotifyBean.setSign_type("RSA");
			// 转换成最终发出的数据
			String send = FormatUtil.beanToHttpString(refundnotifyBean, true);

			refundnotifyCall = new RefundNotifyCall("13");
			refundnotifyCall.setData(send);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("success", refundnotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb, refundnotifyBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_003() {
		Logger.start(true, "11支付宝wap支付同步回调成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("11");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			refundnotifyBean = initRefundNotifyBean(aqb);
			String send = aliPayRefundNotifyToSend(refundnotifyBean, aqb);

			refundnotifyCall = new RefundNotifyCall("11");
			refundnotifyCall.setData(send);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("success", refundnotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb, refundnotifyBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_RefundNotify_004() {
		Logger.start(true, "10支付宝PC支付同步回调重复回调,成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			PaymentDefaultWapper pdw = new PaymentDefaultWapper();
			pdw.updateRefundStatus(ar.getRefundNo(), "1");
			Logger.comment("查询PPPayment信息");
			PPBussinessOrder ppb = pdw.selectPPBussinessorderByTraceId(aqb.getTraceId()).get(0);
			String businessoid = ppb.getBussinessOrderId();
			PPPayment ppm = pdw.selectPPPaymentByBussinessOrderId(businessoid).get(0);
			
			refundnotifyBean = initRefundNotifyBean(aqb);
			String send = aliPayRefundNotifyToSend(refundnotifyBean, aqb);
			refundnotifyCall.addHeader("Mock", "1");
			refundnotifyCall.addHeader("MockResult-Trading-Status","OK");
			refundnotifyCall.addHeader("MockResult-Trading-Body","Proccessed");
			String mockResult=FormatUtil.urlEncode("is_success=T&result_details="+ar.getRefundNo()+"^"+ppm.getInstitutionPaymentId()+"^"+aqb.getPayPrice()+"^SUCCESS");
			refundnotifyCall.addHeader("MockResult-AliPay-Result",mockResult);
			refundnotifyCall = new RefundNotifyCall("10");
			refundnotifyCall.setData(send);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("success", refundnotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb, refundnotifyBean);
			List<Map> list = pdw.selectRefundRequestByTraceId(ar.getRefundNo());
			Logger.verifyEquals(4, list.get(0).get("RefundStatus"), "交易网关返回退款成功RefundStatus4");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_005() {
		Logger.start(true, "60ApplePay支付同步回调成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("60");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			ApplePayRefundNotifyBean applerfn = initApplePayRefundNotifyBean(aqb);
			String send = applePayRefundNotifyToSend(applerfn, aqb);

			refundnotifyCall = new RefundNotifyCall(aqb.getPayType());
			refundnotifyCall.setData(send);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);
			refundnotifyCall.addHeader("MockResult-ApplePay-merId", PaymentProxyCallService.APPLEPAY_MERID);
			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("success", refundnotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb, applerfn);
			PaymentDefaultWapper pdw = new PaymentDefaultWapper();
			List<Map> list = pdw.selectRefundRequestByTraceId(ar.getRefundNo());
			Logger.verifyEquals(4, list.get(0).get("RefundStatus"), "交易网关返回退款成功RefundStatus4");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_006() {
		Logger.start(true, "60ApplePay支付同步回调签名错误失败");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("60");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			ApplePayRefundNotifyBean applerfn = initApplePayRefundNotifyBean(aqb);
			applerfn.setSignature("123");
			String send = applePayRefundNotifyToSend(applerfn, aqb);

			refundnotifyCall = new RefundNotifyCall(aqb.getPayType());
			refundnotifyCall.setData(send);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("fail", refundnotifyCall.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_007() {
		Logger.start(true, "60ApplePay支付同步回调MerId错误失败");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("60");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			ApplePayRefundNotifyBean applerfn = initApplePayRefundNotifyBean(aqb);
			applerfn.setMerId("123");
			String send = applePayRefundNotifyToSend(applerfn, aqb);

			refundnotifyCall = new RefundNotifyCall(aqb.getPayType());
			refundnotifyCall.setData(send);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("fail", refundnotifyCall.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_008() {
		Logger.start(true, "60ApplePay支付同步回调退款批次不存在错误失败");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("60");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			ApplePayRefundNotifyBean applerfn = initApplePayRefundNotifyBean(aqb);
			applerfn.setOrderId("abc");
			String send = applePayRefundNotifyToSend(applerfn, aqb);

			refundnotifyCall = new RefundNotifyCall(aqb.getPayType());
			refundnotifyCall.setData(send);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("fail", refundnotifyCall.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_009() {
		Logger.start(true, "60ApplePay支付同步回调金额不匹配错误失败");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("60");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			ApplePayRefundNotifyBean applerfn = initApplePayRefundNotifyBean(aqb);
			
			applerfn.setTxnAmt("1");
			String send = applePayRefundNotifyToSend(applerfn, aqb);

			refundnotifyCall = new RefundNotifyCall(aqb.getPayType());
			refundnotifyCall.setData(send);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("fail", refundnotifyCall.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	//宝付渠道关闭
	public void Tc_RefundNotify_010() {
		Logger.start(true, "70宝付支付同步回调成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("70");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			BaoFuRefundNotifyBean baofurefundnbean=PaymentProxyCallService.initBaoFuRefundNotifyBean(aqb);
			String baofuexecfund=FormatUtil.beanToGSONString(baofurefundnbean);
			Logger.debug("baofuexecfund:"+baofuexecfund);
			baofuexecfund="data_content="+new BaofooSignature().encryptData(baofuexecfund);

			refundnotifyCall = new RefundNotifyCall("70");
			refundnotifyCall.setData(baofuexecfund);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("OK", refundnotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb, baofurefundnbean);
			PaymentDefaultWapper pdw = new PaymentDefaultWapper();
			List<Map> list = pdw.selectRefundRequestByTraceId(ar.getRefundNo());
			Logger.verifyEquals(4, list.get(0).get("RefundStatus"), "交易网关返回退款成功RefundStatus4");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_011() {
		Logger.start(true, "70支付同步回调签名错误失败");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("70");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			BaoFuRefundNotifyBean baofurefundnbean=PaymentProxyCallService.initBaoFuRefundNotifyBean(aqb);
			String baofuexecfund=FormatUtil.beanToGSONString(baofurefundnbean);
			Logger.debug("baofuexecfund:"+baofuexecfund);
			baofuexecfund="data_content=1"+new BaofooSignature().encryptData(baofuexecfund);

			refundnotifyCall = new RefundNotifyCall("70");
			refundnotifyCall.setData(baofuexecfund);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("fail", refundnotifyCall.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_012() {
		Logger.start(true, "70支付同步回调MerId错误失败");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("70");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			BaoFuRefundNotifyBean baofurefundnbean=PaymentProxyCallService.initBaoFuRefundNotifyBean(aqb);
			baofurefundnbean.setMember_id("111");
			String baofuexecfund=FormatUtil.beanToGSONString(baofurefundnbean);
			Logger.debug("baofuexecfund:"+baofuexecfund);
			baofuexecfund="data_content="+new BaofooSignature().encryptData(baofuexecfund);

			refundnotifyCall = new RefundNotifyCall("70");
			refundnotifyCall.setData(baofuexecfund);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("fail", refundnotifyCall.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_013() {
		Logger.start(true, "70支付同步回调退款批次不存在错误失败");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("70");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			BaoFuRefundNotifyBean baofurefundnbean=PaymentProxyCallService.initBaoFuRefundNotifyBean(aqb);
			baofurefundnbean.setRefund_order_no("222");
			String baofuexecfund=FormatUtil.beanToGSONString(baofurefundnbean);
			Logger.debug("baofuexecfund:"+baofuexecfund);
			baofuexecfund="data_content="+new BaofooSignature().encryptData(baofuexecfund);

			refundnotifyCall = new RefundNotifyCall("70");
			refundnotifyCall.setData(baofuexecfund);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("fail", refundnotifyCall.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_014() {
		Logger.start(true, "70支付同步回调金额不匹配错误失败");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("70");
			aqb.setPayPrice("2.05");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			BaoFuRefundNotifyBean baofurefundnbean=PaymentProxyCallService.initBaoFuRefundNotifyBean(aqb);
			baofurefundnbean.setRefund_amt(String.valueOf(Integer.valueOf(baofurefundnbean.getRefund_amt())-1));
			String baofuexecfund=FormatUtil.beanToGSONString(baofurefundnbean);
			Logger.debug("baofuexecfund:"+baofuexecfund);
			baofuexecfund="data_content="+new BaofooSignature().encryptData(baofuexecfund);

			refundnotifyCall = new RefundNotifyCall("70");
			refundnotifyCall.setData(baofuexecfund);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("fail", refundnotifyCall.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RefundNotify_015() {
		Logger.start(true, "9支付宝花呗支付同步回调成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("9");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());

			refundnotifyBean = initRefundNotifyBean(aqb);

			// 排序加签名
			String src = FormatUtil.beanToHttpString(refundnotifyBean, false);
			String sign = new Sign().SignSHA1WithRSA(src);
			refundnotifyBean.setSign(sign);
			refundnotifyBean.setSign_type("RSA");
			// 转换成最终发出的数据
			String send = FormatUtil.beanToHttpString(refundnotifyBean, true);

			refundnotifyCall = new RefundNotifyCall("9");
			refundnotifyCall.setData(send);

			refundnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			refundnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);

			refundnotifyCall.callService();
			Logger.verifyEquals("success", refundnotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb, refundnotifyBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	private String applePayRefundNotifyToSend(ApplePayRefundNotifyBean applerfb, AcquireOrderBean aqb) throws Exception {
		// TODO Auto-generated method stub
		String plaintext = FormatUtil.beanToHttpString(applerfb, false);
		byte[] signDigest = SecurityUtil.sha1(plaintext, ApplePayConstants.encoding);
        signDigest = Hex.toHexBytes(signDigest, ApplePayConstants.encoding);
        PrivateKey privateKey = RSAUtils.loadPrivateKey(EnvSetup.getData("privateK"));
        byte[] signData = SecurityUtil.signWithSha1Rsa(privateKey, signDigest);
        signData = Base64.getEncoder().encode(signData);
        applerfb.setSignature(new String(signData));
		String send = FormatUtil.beanToHttpString(applerfb, true);
		return send;
	}
	private ApplePayRefundNotifyBean initApplePayRefundNotifyBean(AcquireOrderBean aqb) throws InterruptedException{
		ApplePayRefundNotifyBean aprf=new ApplePayRefundNotifyBean();
		Integer price =new BigDecimal(aqb.getPayPrice()).multiply(new BigDecimal(100)).intValue();
		PPBussinessOrder ppb = new PaymentDefaultWapper().selectPPBussinessorderByOrderId(aqb.getOrderId()).get(0);
		String businessoid = ppb.getBussinessOrderId();
		PPPayment ppm = new PaymentDefaultWapper().selectPPPaymentByBussinessOrderId(businessoid).get(0);
		RefundRequest RefundRequest = new PaymentDefaultWapper().selectRefundRequestByPaymentid(ppm.getPaymentId())
				.get(0);
		aprf.setCurrencyCode("156");
		aprf.setMerId(PaymentProxyCallService.APPLEPAY_MERID);
		aprf.setOrderId(RefundRequest.getRefundBatchNo());
		aprf.setRespCode("00");
		aprf.setTxnAmt(price.toString());
		return aprf;
	}
	private RefundNotifyBean initRefundNotifyBean(AcquireOrderBean aqb) throws Exception {
		refundnotifyBean.setNotify_time(DataManager.getSysTime());
		refundnotifyBean.setNotify_type("batch_refund_notify");
		refundnotifyBean.setNotify_id(TestDataManager.getClientpaymentId());
		try{
		PPBussinessOrder ppb = new PaymentDefaultWapper().selectPPBussinessorderByOrderId(aqb.getOrderId()).get(0);
		String businessoid = ppb.getBussinessOrderId();
		PPPayment ppm = new PaymentDefaultWapper().selectPPPaymentByBussinessOrderId(businessoid).get(0);
		RefundRequest RefundRequest = new PaymentDefaultWapper().selectRefundRequestByPaymentid(ppm.getPaymentId())
				.get(0);
		String result_details = ppm.getInstitutionPaymentId() + "^" + aqb.getPayPrice() + "^SUCCESS"
				+ "$ap.ymt@ymatou.com^2088701734809577^0.01^SUCCESS";
		refundnotifyBean.setResult_details(result_details);
		refundnotifyBean.setBatch_no(RefundRequest.getRefundBatchNo());
		refundnotifyBean.setSuccess_num("1");
		}catch(Exception e){
			Logger.debug(e.getMessage());
		}
		return refundnotifyBean;
	}

	private String aliPayRefundNotifyToSend(RefundNotifyBean refundNotifyBean, AcquireOrderBean aqb) throws Exception {
		Logger.comment("aliPayRefundNotify加签并转换为SendString");
		String send = null;
		if (aqb.getPayType().equals("13")) {
			// 排序加签名
			String src = FormatUtil.beanToHttpString(refundNotifyBean, false);
			String sign = new Sign().SignSHA1WithRSA(src);
			refundNotifyBean.setSign(sign);
			refundNotifyBean.setSign_type("RSA");
			// 转换成最终发出的数据
			send = FormatUtil.beanToHttpString(refundNotifyBean, true);
		} else {
			String src = FormatUtil.beanToHttpString(refundNotifyBean, false);
			String sign = new Sign().signMd5(refundNotifyBean, Sign.autoTestKey);
			refundNotifyBean.setSign(sign);
			refundNotifyBean.setSign_type("MD5");
			send = FormatUtil.beanToHttpString(refundNotifyBean, true);
		}
		return send;
	}

	private void verifyDB(AcquireOrderBean aqb, RefundNotifyBean refundnotifyBean) throws Exception {
		Logger.comment("验证数据库RefundMiscRequestLog");
		String batchno = refundnotifyBean.getBatch_no();
		int size = 0;
		int loop = 0;
		List<RefundMiscRequestLog> rmrl = new ArrayList<RefundMiscRequestLog>();
		while (size == 0 && loop < 3) {
			rmrl = new PaymentDefaultWapper().selectRefundMiscRequestLogByRefundBatchNo(batchno);
			size = rmrl.size();
			loop++;
			Thread.sleep(1000);
		}
		if (rmrl.size() > 0) {
			RefundMiscRequestLog rmr = rmrl.get(0);
			Map rfr = new PaymentDefaultWapper().selectRefundRequestByRefundBatchNo(batchno).get(0);
//			Logger.debug(rfr);
			Logger.verifyEquals(rfr.get("RefundId").toString(), rmr.getCorrelateId(), "验证CorrelateId");
			//TODO Method是如何决定的？ 手工验证
//			Logger.verifyEquals("AliRefund", rmr.getMethod(), "验证Method");
			Logger.verifyEquals(false, rmr.getIsException(), "验证IsException");
		} else {
			throw new Exception("RefundMiscRequestLog没有查到记录:RefundBatchNo:" + batchno);
		}

	}
	private void verifyDB(AcquireOrderBean aqb, ApplePayRefundNotifyBean applepayref) throws Exception {
		Logger.comment("验证数据库RefundMiscRequestLog");
		String batchno = applepayref.getOrderId();
		int size = 0;
		int loop = 0;
		List<RefundMiscRequestLog> rmrl = new ArrayList<RefundMiscRequestLog>();
		while (size == 0 && loop < 3) {
			rmrl = new PaymentDefaultWapper().selectRefundMiscRequestLogByRefundBatchNo(batchno);
			size = rmrl.size();
			loop++;
			Thread.sleep(1000);
		}
		if (rmrl.size() > 0) {
			Logger.debug("RefundMiscRequestLog:"+FormatUtil.beanToGSONString(rmrl));
			RefundMiscRequestLog rmr = rmrl.get(0);
			Map rfr = new PaymentDefaultWapper().selectRefundRequestByRefundBatchNo(batchno).get(0);
//			Logger.debug(rfr);
			Logger.verifyEquals(rfr.get("RefundId").toString(), rmr.getCorrelateId(), "验证CorrelateId");
			//TODO Method是如何决定的？ 手工验证
//			Logger.verifyEquals("AliRefund", rmr.getMethod(), "验证Method");
			Logger.verifyEquals(false, rmr.getIsException(), "验证IsException");
		} else {
			throw new Exception("RefundMiscRequestLog没有查到记录:RefundBatchNo:" + batchno);
		}

	}
	private void verifyDB(AcquireOrderBean aqb, BaoFuRefundNotifyBean refundnotifyBean) throws Exception {
		Logger.comment("验证数据库RefundMiscRequestLog");
		String batchno = refundnotifyBean.getRefund_order_no();
		int size = 0;
		int loop = 0;
		List<RefundMiscRequestLog> rmrl = new ArrayList<RefundMiscRequestLog>();
		while (size == 0 && loop < 3) {
			rmrl = new PaymentDefaultWapper().selectRefundMiscRequestLogByRefundBatchNo(batchno);
			size = rmrl.size();
			loop++;
			Thread.sleep(1000);
		}
		if (rmrl.size() > 0) {
			RefundMiscRequestLog rmr = rmrl.get(0);
			Map rfr = new PaymentDefaultWapper().selectRefundRequestByRefundBatchNo(batchno).get(0);
//			Logger.debug(rfr);
			Logger.verifyEquals(rfr.get("RefundId").toString(), rmr.getCorrelateId(), "验证CorrelateId");
			//TODO Method是如何决定的？ 手工验证
//			Logger.verifyEquals("AliRefund", rmr.getMethod(), "验证Method");
			Logger.verifyEquals(false, rmr.getIsException(), "验证IsException");
		} else {
			throw new Exception("RefundMiscRequestLog没有查到记录:RefundBatchNo:" + batchno);
		}

	}
}
